/*
 * ESPeachyPinata.cs
 *
 * Created on January 4, 2008, 5:27 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * Copyright (C) 2010       Mathijs Miermans
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HexTD
{
	public class ESPeachyPinata : EnemySpriteBase
	{

		private int prevPaintTime;
		private Color bodyColor = new Color(255, 255, 218, 185);
		private Color bodyShellColor = Color.White;
#if PORT
		private Brush bodyStroke;
		private Brush bodyShellStroke;
		private Shape[] bodyShapes;
		private Shape bodyShellShape;
#endif
		private int bodyStep = 0;
		private static float reductionFactor = 0.005f;
		private static string infoText =
				"<h1>Peachy Piñata</h1>" +
				"<p>" +
				"There's a bonus inside, but this piñata is not made out of paper!" +
				"</p>";

		/**
		 * Creates a new instance of ESPeachyPinata
		 */
		public ESPeachyPinata(Game game)
			: base(game, "Peachy Piñata")
		{
			this.damageColor = COLOR.BLUE;
			this.damageColorAnti = COLOR.YELLOW;
		}

		public string getInfostring()
		{
			return infoText +
					"<p><b>Damage Reduction:</b> " + Math.Round(reductionFactor * this.healthMax).ToString(form) + "<br>" +
					"" + base.getInfostring() + "</p>";
		}

		protected void doDamage(int damage)
		{
			long realDamage = (long)Math.Round(damage - reductionFactor * this.healthMax);
			realDamage = Math.Max(realDamage, 1) * 3 / 4;

			if (this.validTarget())
			{
				this.health -= realDamage;
			}
			if (this.health <= 0)
			{
				this.isValidTarget = false;
				this.dead = true;
				this.context.addBonus();
				this.context.addScore(this.price);
				this.context.doReceive(this.price);
				this.context.removeEnemy();
			}
			this.fireStatusChangedEvent();
		}

		public void resetScale()
		{
#if PORT
			base.resetScale();
			Cache cache = this.context.getCache();


			if (cache.hasShapeArr("ESPePiShapes" + this.size.letter)) {
				this.bodyShapes = cache.getShapeArr("ESPePiShapes" + this.size.letter);
				this.bodyShellShape = cache.getShape("ESPePiShape" + this.size.letter);
			} else {
				float scale = this.context.paintScale * this.size.scaleMod;
				this.bodyShapes = new Shape[30];

				float r0 = scale / 6;
				float dr1 = r0 / 2;
				float dr2 = dr1 / this.bodyShapes.length;
				int max = this.bodyShapes.length;
				for (int i = 0; i < max; i++) {
					Area a = new Area(Geometry.createCircle(0, 0, r0 + max * dr2));
					a.exclusiveOr(new Area(Geometry.createCircle(0, 0, r0 - dr1 * 0 + i * dr2)));
					a.exclusiveOr(new Area(Geometry.createCircle(0, 0, r0 - dr1 * 1 + i * dr2)));
					a.exclusiveOr(new Area(Geometry.createCircle(0, 0, r0 - dr1 * 2 + i * dr2)));
					this.bodyShapes[i] = a;
				}
				this.bodyShellShape = new Area(Geometry.createCircle(0, 0, r0 + max * dr2));
				cache.putShapeArr("ESPePiShapes" + this.size.letter, this.bodyShapes);
				cache.putShape("ESPePiShape" + this.size.letter, this.bodyShellShape);
			}
			this.bodyStroke = new Brush(1f * this.context.strokeScale, Brush.CAP_ROUND, Brush.JOIN_ROUND);
			this.bodyShellStroke = new Brush(2f * this.context.strokeScale, Brush.CAP_ROUND, Brush.JOIN_ROUND);
#endif
		}

		public override void Draw(GameTime gameTime)
		{
#if PORT
			if (this.inactive || this.dead) {
				this.prevPaintTime = gameTime;
			} else {
				Brush defaultStroke = g2.getStroke();
				this.bodyStep += gameTime - this.prevPaintTime;
				if (this.bodyStep >= this.bodyShapes.Length) {
					this.bodyStep = 0;
				}
				this.prevPaintTime = gameTime;

				RotateTransform saveXform = g2.getTransform();
				g2.transform(atTranslate);

				if (this.shielded) {
					this.paintShield_bottom(g2);
				}
				this.paintHealthLine(g2);

				g2.setColor(this.bodyColor);
				g2.setStroke(this.bodyStroke);
				g2.draw(this.bodyShapes[this.bodyStep]);
				g2.setColor(this.bodyShellColor);
				g2.setStroke(this.bodyShellStroke);
				g2.draw(this.bodyShellShape);
				g2.setTransform(saveXform);

				if (this.shielded) {
					g2.transform(atTranslate);
					this.paintShield_top(g2);
					g2.setTransform(saveXform);
				}

				base.paintSelected(g2);

				g2.setStroke(defaultStroke);
			}
#endif
		}
	}
}